# 全局变量 存放所有房源信息
from random import choice
from my_tools import *

houses = [{"id": 2, "name": "tim", "phone": "113", "address": "北京", "rent": 100, "state": "未出租"}]

# 全局遍历id_count 记录当前房屋id
id_count = 1


# 显示房屋列表
def show_house():
    print("--------------房屋列表------------------")
    print("编号\t\t房主\t\t电话\t\t地址\t\t月租\t\t状态(未出租/已出租)")
    # 遍历houses列表
    for house in houses:
        # 取出house中的value
        for value in house.values():
            print(value, end="\t\t")
        # 输出换行
        print()
    print("--------------房屋列表显示完毕------------------")


# 添加房屋信息
def add_house():
    print("--------------添加房屋信息------------------")
    print("请输入房源信息:")
    name = input("请输入房主姓名:")
    phone = input("请输入房屋电话:")
    address = input("请输入房屋地址:")
    rent = int(input("请输入房屋租金:"))
    state = input("状态:")
    # 在函数内想要使用全局变量,需要使用global关键字
    global id_count
    id_count += 1
    # 构建房屋信息对应的字典 加入到全局house列表中
    house = {"id": id_count, "name": name, "phone": phone, "address": address, "rent": rent, "state": state}
    houses.append(house)
    print("---------------添加成功--------------------")


# 根据id删除房屋信息
def delete_house():
    print("--------------删除房屋信息------------------")
    id = int(input("请输入要删除的房源编号:(-1退出):"))
    if id == -1:
        print("放弃删除房屋信息")
        return

    # 如果输入的不是y或者n,则一直提示重新输入
    # while True:
    #     choice = input("请输入你的选择(Y/N):")
    #     if choice.lower() == "y" or choice.lower() == "n":
    #         break
    choice = read_confirm_select()
    if choice == 'y':
        # 根据id去查找houses列表中的房屋信息 看看是否存在
        house = find_by_id(id)
        if house:
            # 执行删除
            houses.remove(house)
            print("---------------删除成功--------------------")
        else:
            print("你输入的房屋编号不存在，删除失败")
            return
    else:
        print("放弃删除房屋信息")
        return


# 根据id查询房屋信息
def find_by_id(find_id):
    # 遍历houses列表
    for house in houses:
        if house["id"] == find_id:
            return house
    # 如果没有return 默认返回None
    return None


# 退出系统
def exit_system():
    choice = read_confirm_select()
    if choice == 'y':
        return True
    else:
        return False


def query_house():
    print("--------------查询房屋信息------------------")
    find_id = int(input("请输入要查询的房源编号:"))
    house = find_by_id(find_id)
    # 调用函数返回对应的房屋信息
    if house:
        print("编号\t\t房主\t\t电话\t\t地址\t\t月租\t\t状态(未出租/已出租)")
        for value in house.values():
            print(value, end="\t\t")
    else:
        print("你输入的房屋编号不存在")


# 修改房屋信息
def update_house():
    print("--------------修改房屋信息------------------")
    update_id = int(input("请输入要修改的房源编号(-1退出):"))
    if update_id == -1:
        print("放弃修改房屋信息")
        return
    # 根据id查找对应的房源信息
    house = find_by_id(update_id)
    if not house:
        print("你输入的房屋编号不存在")
        return
    # 注意如果用户直接回车，表示不修改当前这个信息 保留原来的值
    # name = input(f"姓名:({house['name']})")
    # if len(name) > 0:
    #     # 表示将接收到的name，赋值给 house字典赋值给key = "name"对应的值
    #     house['name'] = name
    house['name'] = read_str(f"姓名:({house['name']}):", house['name'])

    # phone = input(f"电话:{house['phone']}")
    # if len(phone) > 0:
    #     house['phone'] = phone
    house['phone'] = read_str(f"电话:{house['phone']}", house['phone'])

    # address = input(f"地址:{house['address']}")
    # if len(address) > 0:
    #     house['address'] = address
    house['address'] = read_str(f"地址:{house['address']}", house['address'])

    # rent = input(f"月租:{house['rent']}")
    # if len(rent) > 0:
    #     house['rent'] = int(rent)
    house['rent'] = int(read_str(f"月租:{house['rent']}", house['rent']))

    # state = input(f"状态:{house['state']}")
    # if len(state) > 0:
    #     house['state'] = state
    house['state'] = read_str(f"状态:{house['state']}", house['state'])

    print("---------------修改成功--------------------")


def main_menu():
    print()
    # 显示主菜单
    print("--------------房屋信息管理系统--------------")
    print("--------------1. 新增房源------------------")
    print("--------------2. 查询房源------------------")
    print("--------------3. 删除房源------------------")
    print("--------------4. 修改房源------------------")
    print("--------------5. 房屋列表------------------")
    print("--------------6. 退出  --------------------")
    print("------------------------------------------")
    print("请输入你的选择:(1-6)")
